package com.stock_t.controller;

import com.stock_t.dto.ApiResponse;
import com.stock_t.dto.HistoricalPositionDTO;
import com.stock_t.dto.HistoricalPositionPageDTO;
import com.stock_t.service.HistoricalPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/historical-position")
public class HistoricalPositionController {

    @Autowired
    private HistoricalPositionService historicalPositionService;

    /**
     * 分页查询历史持仓记录
     */
    @GetMapping("/page")
    public ApiResponse<HistoricalPositionPageDTO> getHistoricalPositionPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String stockCode,
            @RequestParam(required = false, defaultValue = "date") String sortType,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder) {
        
        log.info("分页查询历史持仓记录，当前页：{}，每页数量：{}，股票代码：{}，排序类型：{}，排序顺序：{}", 
                current, size, stockCode, sortType, sortOrder);
        
        return historicalPositionService.getHistoricalPositionPage(current, size, stockCode, sortType, sortOrder);
    }

    /**
     * 上传历史持仓图片
     */
    @PostMapping("/upload")
    public ApiResponse<String> uploadHistoricalPosition(
            @RequestParam("file") MultipartFile file,
            @RequestParam("positionDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date positionDate,
            @RequestParam("stockCode") String stockCode,
            @RequestParam("stockName") String stockName) {
        
        log.info("上传历史持仓图片，持仓日期：{}，股票代码：{}，股票名称：{}", 
                positionDate, stockCode, stockName);
        
        return historicalPositionService.uploadHistoricalPosition(file, positionDate, stockCode, stockName);
    }

    /**
     * 删除历史持仓记录
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteHistoricalPosition(@PathVariable("id") Long id) {
        log.info("删除历史持仓记录，ID：{}", id);
        return historicalPositionService.deleteHistoricalPosition(id);
    }

    /**
     * 批量删除历史持仓记录
     */
    @DeleteMapping("/batch")
    public ApiResponse<String> batchDeleteHistoricalPosition(@RequestBody List<Long> ids) {
        log.info("批量删除历史持仓记录，ID列表：{}", ids);
        return historicalPositionService.batchDeleteHistoricalPosition(ids);
    }

    /**
     * 获取单条历史持仓记录
     */
    @GetMapping("/{id}")
    public ApiResponse<HistoricalPositionDTO> getHistoricalPositionById(@PathVariable("id") Long id) {
        log.info("获取单条历史持仓记录，ID：{}", id);
        return historicalPositionService.getHistoricalPositionById(id);
    }
}